package com.lin.operatorlogrecord.compoment;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author: chenghao.lin
 * @Date: 2023/09/21
 * @Description: 静态获取Bean
 */
@Component
public class SpringBeanContext implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringBeanContext.applicationContext = applicationContext;
    }

    /**
     * 静态获取Bean方法
     */
    public static <T> T getSpringBean(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }
}
